#include"BlockQueue.hpp"
void* Consumer(void* args)
{
   auto bq = static_cast<BlockQueue<int>*>(args);
   int x;
   sleep(2);
   while(true)
   {
    //消费
     x = bq->pop();
     std::cout << "消费了一个数据: " << x << "此时还有: " << bq->size() << "个数据" <<
    " pthread id: " << pthread_self() << std::endl;
     sleep(1);
   } 
}

void* Producter(void* args)
{
    auto bq = static_cast<BlockQueue<int>*>(args);
    int data = 0;
    while(true)
    {
        //生产
        data++;
        bq->push(data);
        std::cout << "生产了一个数据: " << data << "此时还有: " << bq->size() << "个数据" << 
        " pthread id: " << pthread_self() << std::endl;
        sleep(2);
    }
}

int main()
{
    srand(time(0));
    //可以传递对象
    BlockQueue<int>* bq = new BlockQueue<int>();
    pthread_t c[3],p[3];
    for(int i = 0;i < 3 ;++i)
    pthread_create(c + i,nullptr,Consumer,(void*)bq);
    for(int i = 0;i < 3 ;++i)
    pthread_create(p + i,nullptr,Producter,(void*)bq);
    for(int i = 0;i < 3 ;++i)
    pthread_join(*(c + i),nullptr);
    for(int i = 0;i < 3 ;++i)
    pthread_join(*(p + i),nullptr);

    delete bq;
    return 0;
}